<?php
class Answer extends AppModel {
	var $name = 'Answer';
	var $displayField = 'id';
	
	var $validate = array(
		'question_id' => array(
			'numeric' => array(
				'rule' => array('numeric'),
				//'message' => 'Your custom message here',
				//'allowEmpty' => false,
				//'required' => false,
				//'last' => false, // Stop validation after this rule
				//'on' => 'create', // Limit validation to 'create' or 'update' operations
			),
		),
		'user_id' => array(
			'numeric' => array(
				'rule' => array('numeric'),
				//'message' => 'Your custom message here',
				//'allowEmpty' => false,
				//'required' => false,
				//'last' => false, // Stop validation after this rule
				//'on' => 'create', // Limit validation to 'create' or 'update' operations
			),
		),
	);
	//The Associations below have been created with all possible keys, those that are not needed can be removed

	var $belongsTo = array(
		'Question' => array(
			'className' => 'Question',
			'foreignKey' => 'question_id',
			'conditions' => '',
			'fields' => '',
			'order' => ''
		),
		'User' => array(
			'className' => 'User',
			'foreignKey' => 'user_id',
			'conditions' => '',
			'fields' => '',
			'order' => ''
		)
	);
	
	function sumOfQuestions($ids,$userId)
	{
		$fields = array(' SUM(Answer.mark) as total');
		$conditions = array('Answer.user_id'=>$userId,'Answer.question_id'=>$ids);
		if($total = $this->find('first',array('fields'=>$fields,'conditions'=>$conditions)))		
		{		
			return (trim($total[0]['total'])=='')?'0':$total[0]['total'];
		}
			return '0';
	}
	
	function getEssayAnswer($userId,$questionId)
	{
		$conditions = array('Answer.user_id'=>$userId,'Answer.question_id'=>$questionId);
		return $this->find('first',array('conditions'=>$conditions));
	}
}
